import Vue from 'vue'
import store from '@/store'

import UniSocketPromise from "@i5920/uni-socket-promise"
Vue.prototype.timer = null;

Vue.prototype.$initSocket = () => {
	getApp().globalData.socket = new UniSocketPromise({
		url: Vue.prototype.$socketUrl + "/websocket/message/" + store.state
			.userInfo.appUserId,
		heartTime: 5 * 60 * 1000, // 心跳时间间隔
		isHeartData: true, // 是否发送心跳
		isReconnect: true, // 是否断线重连
		reConnectTime: 3000, // 断线重连检测时间间隔
		// debug: process.env.NODE_ENV === "development", // debug
		onSocketOpen: header => {},
		onSocketError: res => {},
		onSocketClose: res => {},
		onSocketMessageAfter: (data, pool) => {
			console.log("-------**********************1-----" + JSON.stringify(data));
			if (data.error === 0) {

				pool.resolve(data);
			} else {
				pool.reject(data);
			}
		},
		onSocketMessageBefore: data => {
			console.log("-------***********************2-----" + JSON.stringify(data));
			uni.$emit("socketInfo", data)
		}
	});


}







Vue.prototype.$socketSend = () => {

	Vue.prototype.timer = setInterval(() => {
		getApp().globalData.socket.send("name", {
			"longitude": Vue.prototype.$longitude(),
			"latitude": Vue.prototype.$latitude(),
			"msgType": '0'
		})
	}, 10000);

}

Vue.prototype.$socketSendClose = () => {
	if (Vue.prototype.$role == 0) {
		Vue.prototype.timer = null;
	} else {
		if (Vue.prototype.$isLogin()) {
			Vue.prototype.$u.api.getUnderWayOrderNum().then(res => {
				if (res.code == 200) {
					if (res.data == 0) {
						Vue.prototype.timer = null;
					}
				}
			})
		}

	}





}